<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    img{
      width: 2vw;
      /* height: 2vh; */
    }
  </style>
</head>
<body>
  <ul id="level-1">
    <li>
      <div>
        Item 1
      </div>
      <div>
        <img src="./xy-active.png" alt="show children" class="toggle-item" data-index="0"/>
      </div>
      <ul class="level-2">
        <li>
          <div>
            Item 1-1
          </div>
          <div>
            <img src="./xy-active.png" alt="show children" class="toggle-sub-item" data-parent-index="0" data-sub-index="0">
          </div>
        </li>
        <li>
          <div>
            Item 1-2
          </div>
          <div>
            <img src="./xy-active.png" alt="show children" class="toggle-sub-item" data-parent-index="0" data-sub-index="1">
          </div>
        </li>
      </ul>
    </li>
    <li>
      <div>
        Item 2
      </div>
      <div>
        <img src="./xy-active.png" alt="show children" class="toggle-item" data-index="1">
      </div>
      <ul class="level-2">
        <li>
          <div>
            Item 2-1
          </div>
          <div>
            <img src="./xy-active.png" alt="show children" class="toggle-sub-item" data-parent-index="1" data-sub-index="0">
          </div>
        </li>
        <li>
          <div>
            Item 2-2
          </div>
          <div>
            <img src="./xy-active.png" alt="show children" class="toggle-sub-item" data-parent-index="1" data-sub-index="1">
          </div>
        </li>
      </ul>
    </li>
  </ul>
  
  
 
</body>
<script>
  // 监听二级列表项的切换按钮
  $('.toggle-item').on('click', function() {
    const index = $(this).data('index'); // 获取当前列表项的索引
    
    // 切换二级列表的显示和隐藏状态
    $(`#level-1 li:nth-of-type(${parseInt(index) + 1}) .level-2`).toggleClass('show');
    
    // 切换当前切换按钮的样式
    $(this).toggleClass('active');
    
    // 遍历所有子项的状态，并与父级列表项同步
    $(`#level-1 li:nth-of-type(${parseInt(index) + 1}) .toggle-sub-item`).each(function() {
      const parentIndex = $(this).data('parent-index');
      const subIndex = $(this).data('sub-index');
      
      $(this).toggleClass('active');
      $(`#level-1 li:nth-of-type(${parseInt(parentIndex) + 1}) .level-2 li:nth-of-type(${parseInt(subIndex) + 1}) .level-3`).toggleClass('show');
    });
  });
  
  // 监听三级列表项的切换按钮
  $('.toggle-sub-item').on('click', function() {
    // 切换三级列表的显示和隐藏状态
    const parentIndex = $(this).data('parent-index');
    const subIndex = $(this).data('sub-index');
    $(`#level-1 li:nth-of-type(${parseInt(parentIndex) + 1}) .level-2 li:nth-of-type(${parseInt(subIndex) + 1}) .level-3`).toggleClass('show');
    
    // 切换当前切换按钮的样式
    $(this).toggleClass('active');
    
    // 判断所有子项是否都选中，如果是则更新二级列表项的状态
    const subItems = $(`#level-1 li:nth-of-type(${parseInt(parentIndex) + 1}) .toggle-sub-item`);
    let checkAll = true;
    subItems.each(function() {
      if (!$(this).hasClass('active')) {
        checkAll = false;
      }
    });
    $(`#level-1 li:nth-of-type(${parseInt(parentIndex) + 1}) .toggle-item`).toggleClass('active', checkAll);
    $(`#level-1 li:nth-of-type(${parseInt(parentIndex) + 1}) .level-2`).toggleClass('show', checkAll);
  });
</script>

</html>